package cn.dxs.adt.utils;

import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 文件操作工具类
 */
public class FileUtil {

    private static final String TAG = FileUtil.class.getSimpleName();


    /**
     * 文件保存在SD卡上
     * @param name 文件名称
     * @param content 文件内容
     * @throws Exception
     */
    public static void saveToSD(String name, String content){
        //获取外部设备
        //Environment.getExternalStorageDirectory() 获取SD的路径
        File file=new File(Environment.getExternalStorageDirectory(),name);
        FileOutputStream outStream= null;
        try {
            outStream = new FileOutputStream(file);
            //写入文件
            try {
                outStream.write(content.getBytes());
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件夹(有则不创建，无则创建)
     *
     * @param name
     *            文件夹名称
     * @return true:创建成功或者已经存在;false:系统没有SDCard
     */
    private static boolean createFolder(String name) {
        boolean bResult = false;

        if (Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) {
            File file = new File(name).getParentFile();
            // 如果目录不存在则创建之
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            bResult = true;
        } else {
            bResult = false;
        }

        return bResult;
    }

    /**
     * 复制单个文件
     *
     * @param oldPath$Name String 原文件路径+文件名 如：data/user/0/com.test/files/abc.txt
     * @param newPath$Name String 复制后路径+文件名 如：data/user/0/com.test/cache/abc.txt
     * @return <code>true</code> if and only if the file was copied;
     * <code>false</code> otherwise
     */
    public static boolean copyFile(String oldPath$Name, String newPath$Name) {
        try {
            File oldFile = new File(oldPath$Name);
            if (!oldFile.exists()) {
                L.e(TAG, "copyFile:  oldFile not exist.");
                return false;
            } else if (!oldFile.isFile()) {
                L.e(TAG, "copyFile:  oldFile not file.");
                return false;
            } else if (!oldFile.canRead()) {
                L.e(TAG, "copyFile:  oldFile cannot read.");
                return false;
            }

            if (!createFolder(newPath$Name)) {
                return false;
            }

            FileInputStream fileInputStream = new FileInputStream(oldPath$Name);//读入原文件
            FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name);
            byte[] buffer = new byte[1024];
            int byteRead;
            while ((byteRead = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, byteRead);
            }
            fileInputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 复制文件夹及其中的文件
     *
     * @param oldPath String 原文件夹路径 如：data/user/0/com.test/files
     * @param newPath String 复制后的路径 如：data/user/0/com.test/cache
     * @return <code>true</code> if and only if the directory and files were copied;
     * <code>false</code> otherwise
     */
    public static boolean copyFolder(String oldPath, String newPath) {
        try {
            File newFile = new File(newPath);
            if (!newFile.exists()) {
                if (!newFile.mkdirs()) {
                    L.e(TAG, "copyFolder: cannot create directory.");
                    return false;
                }
            }
            File oldFile = new File(oldPath);
            String[] files = oldFile.list();
            File temp;
            for (String file : files) {
                if (oldPath.endsWith(File.separator)) {
                    temp = new File(oldPath + file);
                } else {
                    temp = new File(oldPath + File.separator + file);
                }

                if (temp.isDirectory()) {//如果是子文件夹
                    copyFolder(oldPath + "/" + file, newPath + "/" + file);
                } else if (!temp.exists()) {
                    L.e(TAG, "copyFolder:  oldFile not exist.");
                    return false;
                } else if (!temp.isFile()) {
                    L.e(TAG, "copyFolder:  oldFile not file.");
                    return false;
                } else if (!temp.canRead()) {
                    L.e(TAG, "copyFolder:  oldFile cannot read.");
                    return false;
                } else {
                    FileInputStream fileInputStream = new FileInputStream(temp);
                    FileOutputStream fileOutputStream = new FileOutputStream(newPath + "/" + temp.getName());
                    byte[] buffer = new byte[1024];
                    int byteRead;
                    while ((byteRead = fileInputStream.read(buffer)) != -1) {
                        fileOutputStream.write(buffer, 0, byteRead);
                    }
                    fileInputStream.close();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
